package org.tissou.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.tissou.client.Panel_Premier.Panel_Premier;
import org.tissou.client.Panel_Premier.Panel_PrePremier;
import org.tissou.client.Services.ProprietaireInfos;
import org.tissou.client.Services.InformationsGlobales;
import org.tissou.client.Services.EnsemblePropriosService;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Manu
 * Date: 18 janv. 2009
 * Time: 09:57:24
 * To change this template use File | Settings | File Templates.
 */
public class ComptesUI implements EntryPoint {

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {

    final String qui = Window.Location.getParameter("qui");

    if (qui == null || qui.equals("")) {
      afficherPanelPrePremier();
      return;
    }

    EnsemblePropriosService.App.getInstance().donnerListeProprietaires(new AsyncCallback<List<ProprietaireInfos>>() {
      public void onFailure(Throwable caught) {
        afficherPanelPrePremier();
      }

      public void onSuccess(List<ProprietaireInfos> result) {
        for (ProprietaireInfos proprio : result) {
          if (proprio.getNom().equalsIgnoreCase(qui)) {
            InformationsGlobales.personneLoguee = proprio;
            Panel_Premier pp = new Panel_Premier();
            pp.afficherDans(RootPanel.get());
            return;
          }
        }

        // Qui n'etait pas valide
        afficherPanelPrePremier();
      }
    });
  }

  private void afficherPanelPrePremier() {
    Panel_PrePremier p = new Panel_PrePremier();
    p.afficherDans(RootPanel.get());
  }
}
